package com.cdream.basic.algorithm.leetcode;

import cn.hutool.core.collection.CollUtil;

import java.util.*;
import java.util.stream.Collectors;

public class TreeTraversal {
    private List<TreeNode> res = new ArrayList<>();
    /**********************前序遍历 start**************************/
    /**
     * 递归
     */
    public void preorderTraversal(TreeNode root) {
        if (root == null) {
            return;
        }
        res.add(root);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
    }

    /**
     * 迭代
     */
    public void preorderTraversal2(TreeNode root) {
        if (root == null) {
            return;
        }
        LinkedList<TreeNode> linkedList = new LinkedList<>();
        linkedList.push(root);
        while (!linkedList.isEmpty()) {
            TreeNode cur = linkedList.pop();
            res.add(cur);
            if (cur.right != null) {
                linkedList.push(cur.right);
            }
            if (cur.left != null) {
                linkedList.push(cur.left);
            }
        }
    }
    /**********************前序遍历 end**************************/

    /**********************中序遍历 start**************************/
    /**
     * 递归
     */
    public void inorderTraversal(TreeNode root) {
        if (root == null) {
            return;
        }
        inorderTraversal(root.left);
        res.add(root);
        inorderTraversal(root.right);
    }

    public void inorderTraversal2(TreeNode root) {
        LinkedList<TreeNode> treeNodes = new LinkedList<>();
        while (root != null || !treeNodes.isEmpty()) {
            while (root != null) {
                treeNodes.push(root);
                root = root.left;
            }
            root = treeNodes.poll();
            res.add(root);
            root = root.right;
        }
    }
    /**********************中序遍历 end**************************/
    /**********************后序遍历 start**************************/
    /**
     * 递归
     */
    public void postorderTraversal(TreeNode root) {
        if (root == null) {
            return;
        }
        postorderTraversal(root.left);
        postorderTraversal(root.right);
        res.add(root);
    }

    public void postorderTraversal2(TreeNode root) {
        if (root == null) {
            return;
        }
        LinkedList<TreeNode> treeNodes = new LinkedList<>();
        treeNodes.add(root);
        while (!treeNodes.isEmpty()) {
            TreeNode node = treeNodes.pop();
            res.add(node);
            if (node.left != null) {
                treeNodes.push(node.left);
            }
            if (node.right != null) {
                treeNodes.push(node.right);
            }
        }
        Collections.reverse(res);
    }
    /**********************后序遍历 end**************************/

    public static void main(String[] args) {
        String s = "abc你";
        System.out.println(Arrays.toString(s.getBytes()));
        byte[] bytes = {-28, -67, -96};
        String s1 = new String(bytes);
        System.out.println(s1);
        System.out.println((byte)228);
    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
